// pages/mall/index.js
import api from '../../request/api.js';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopList:[],
    pageNum:1,
    pageSize:10,
    isEnd:false,
    cateList: [
      { text: '所有分类', value: 0 }
    ],
    option2: [
      { text: '最新', value: 1 },
      { text: '最热', value: 2 }
    ],
    cate: 0, //类别
    sort: 1, //排序
    checked: false,
    credit:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

   /***
   * 获取商品分类
   */
  async getCredit(){
    var that = this;
    const {data:res} = await api('GET','Recycle/user/getMyCredit',{
      token : wx.getStorageSync('token'),
    })
    if(res.code == 200){
      that.setData({
        credit: res.data
      })
    }
  },

  /***
   * 获取商品分类
   */
  async getCateList(){
    var that = this;
    const {data:res} = await api('GET','Recycle/goods/getGoodsCategory',{})
    if(res.code == 200){
      that.setData({
        cateList: that.cateList(res.data)
      })
    }
  },
  /**
   * 获取商品列表
   */
  async getShopList(pageNum){
    var that = this;
    let user_id = wx.getStorageSync('userInfo').user_id
    const { data: res } = await api('GET', 'Recycle/goods/getAllGoods',{
      token: wx.getStorageSync('token'),
      pageNum:pageNum,
      pageSize:that.data.pageSize,
      category_id:that.data.cate > 0 ? that.data.cate : '',
      sort:that.data.sort,
      user_id : that.data.checked ? user_id : ''
    })
    if(res.code ===200){
      //判断是否存在下一页
      if(res.data.length <= 10 && that.data.pageNum > 1){
        that.setData({
          isEnd:true
        })
      }
      if(res.data.length > 0){
        that.setData({
          pageNum:that.data.pageNum+1
        })
      }
      this.setData({
        shopList: that.listArray(res.data)
      })
    }
  },
  exchange(e){
    let that = this;
    that.setData({
      pageNum:1,
      isEnd:false,
      shopList:[],
      checked:e.detail
    })
    that.getShopList(1);
  },
  // 列表数据处理
  listArray(res){
    var that = this;
    let list = that.data.shopList;
    for (let i = 0; i < res.length; i++) {
      list.push({
        id : res[i].goods_id,
        image_src :  app.globalData.imageUrl+res[i].goods_images[0],
        title : res[i].goods_name,
        integral : res[i].credit,
        cash : res[i].money
      })
    };
    return list;  
  },

  // 列表数据处理
  cateList(res){
    var that = this;
    var list = [];
    for (let i = 0; i < res.length; i++) {
      list.push({
        text : res[i].category_name,
        value : res[i].category_id
      })
    };
    return list;  
  },
  //获取下拉菜单点击的值
  setCate(e) {
    let that = this;
    that.setData({
      cate:e.detail,
      pageNum:1,
      isEnd:false,
      shopList:[]
    })
    that.getShopList(1);
  },
  //获取下拉菜单点击的值
  setSort(e) {
    let that = this;
    that.setData({
      sort:e.detail,
      pageNum:1,
      isEnd:false,
      shopList:[]
    })
    that.getShopList(1);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.getCateList();
    that.getShopList(that.data.pageNum);
    that.getCredit();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if(that.data.isEnd){
      console.error('到底了');return;
    }
    that.getShopList(that.data.pageNum);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})